//
//  ViewController.m
//  15-UICollectionView-LifeCycle
//
//  Created by mafeng on 16/8/3.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
#define kCollectionViewCell @"reuse"
#define kRandomColor [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1]
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collection;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds) - 5) / 2 , 120);
    flow.minimumLineSpacing = 5;
    flow.minimumInteritemSpacing = 5;
    
    self.collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flow];
    _collection.delegate = self;
    _collection.dataSource = self;
    [self.view addSubview:_collection];
    [_collection registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCell];
    
    
    [self addLongPressGesture];
    // 关于CollectionViewLifeCycle
    // 1. numberOfSectionInCollection
    // 2. numberOfItemsInSection
    // 注意: 3-4循环执行
    // 3. prepareForReuse(从重用池取出时调用)
    // 4. cellForItemAtIndexPath
    // 5. collectionView willDisplayCell
    // 当cell完全离开屏幕时,
    // didEndDisplayingCell
    // iOS10以前
    // 出屏幕立即返回, 依旧进入3->4->5
    // iOS10以后
    // 出屏幕以后返回, 会执行4->5, 不会执行3, 提高了效率
}
#pragma mark -- collectionView LifeCycle
// 区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
// 区里的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
     NSLog(@"%ld---%s", section, __FUNCTION__);
    return 100;
}
// 从重用池取出cell
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCell forIndexPath:indexPath];
     NSLog(@"%@---%s", indexPath, __FUNCTION__);
    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1];
    return cell;
}
// iOS 8.0以后推出, cell将要显示在屏幕上
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
     NSLog(@"%@---%s", indexPath, __FUNCTION__);
}
// iOS 8.0以后推出, cell已经离开屏幕时
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@---%s", indexPath, __FUNCTION__);
}



//
- (void)handleLongPress:(UILongPressGestureRecognizer *)longPress {
    
    // 根据长按手势的状态, 调用item的移动方法.
    switch (longPress.state) {
        case UIGestureRecognizerStateBegan:
        {
            // 根据手势在View上的点, 找到对应item的indexpath
            NSIndexPath *beginIndexPath = [self.collection indexPathForItemAtPoint:[longPress locationInView:self.collection]];
            // 如果没有点中item, 直接退出.
            if (beginIndexPath == nil) {
                return;
            }
            [self.collection beginInteractiveMovementForItemAtIndexPath:beginIndexPath];
            break;
        }
        case UIGestureRecognizerStateChanged:
        {
            // 随着手势移动, item位置不断更新.
            [self.collection updateInteractiveMovementTargetPosition:[longPress locationInView:self.collection]];
            break;
        }
        case UIGestureRecognizerStateEnded:
        {
            // 手势结束, item移动结束.
            [self.collection endInteractiveMovement];
            break;
        }
        default:
            [self.collection cancelInteractiveMovement];
            break;
    }
}

/** 哪些item 可以移动. */
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

/** 移动item调用此方法. */
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    
    // 更新数据源!!!
    
}


#pragma mark - CollectionView Delegate
/** 选中item之后,调用此方法. */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"%ld, %ld", indexPath.section, indexPath.item);
}

- (void)addLongPressGesture {
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    
    [self.collection addGestureRecognizer:longPress];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
